<template>
  <div>
    我是孙子组件
    <el-button @click="sendValueToGrand">调用爷爷组件的方法</el-button>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="活动名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="活动区域" prop="region">
        <el-select v-model="ruleForm.region" placeholder="请选择活动区域">
          <el-option label="区域一" value="shanghai"></el-option>
          <el-option label="区域二" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="活动时间" required>
        <el-col :span="11">
          <el-form-item prop="date1">
            <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1" style="width: 100%;"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
    </el-form>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
      <el-form-item label="活动形式" prop="desc">
        <el-input type="textarea" v-model="ruleForm.desc"></el-input>
      </el-form-item>
    </el-form>
    <el-button @click="submit">提交</el-button>
  </div>
</template>
<script>
  export default {
    inject:['getSonValue'],
    mounted() {
      this.$nextTick(function (){
        this.$on('sonFunc',this.sonFunc)
      })
    },
    data(){
      return{
        ruleForm: {
          name: '',
          region: '',
          type:''
        },
        rules:{
          name: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '请选择活动区域', trigger: 'change' }
          ],
          desc: [
            { required: true, message: '请填写活动形式', trigger: 'blur' }
          ]
        }
      }
    },
    methods:{
      sendValueToGrand(){
          this.getSonValue('我是来自孙子组件中的值')
      },
      sonFunc(){
        console.log("我是孙子组件的值") ;
        this.submit() ;
      },
      async submit() {
        let res = await Promise.all([this.validateForm('ruleForm'),this.validateForm('ruleForm2')]) ;
        console.log("--------res:",res) //[false,false]
        let flag =  res.every(item => item)
        console.log("-----flag:",flag)
        return flag ;
      },
      validateForm(formRef){
        return  new Promise((resolve,rejecrt) => {
          this.$refs[formRef].validate((valid)=>{
            console.log("valid:",valid)
            resolve(valid)
          })
        })
      },
    },
  }
</script>
<style scoped>

</style>